package com.aube.interact.fighter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by hill on 10/24.
 */
public class ProgressView extends View {

    private final Paint paint;

    private float mProgressRatio = 0;
    private float mCurrentRatio = 0.5f;
    private int mProgressColor = 0xffcc0000;
    private int mDefaultColor = 0xff5f5f5f;
    private int mCurrentColor = 0xffcccccc;
    private int mProgressHeight = 4;

    public ProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.paint = new Paint();
        this.paint.setAntiAlias(true); //消除锯齿

        mProgressColor = getResources().getColor(R.color.progress_color);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();

        int mCurrent = (int) (width * mCurrentRatio);
        int mProgress = (int) (width * mProgressRatio);

        int top = (height - mProgressHeight)/2;
        int bottom = (height + mProgressHeight)/2;

        //画整条
        paint.setColor(mDefaultColor);
        canvas.drawRect(0, top, width, bottom, paint);

        //画进度
        paint.setColor(mProgressColor);
        canvas.drawRect(0, top, mProgress, bottom, paint);

        //画当前
        paint.setColor(mCurrentColor);
        canvas.drawLine(mCurrent, 0, mCurrent+1, height, paint);
    }

    public void updateProgress(float ratio, float current) {
        this.mProgressRatio = ratio;
        this.mCurrentRatio = current;
        invalidate();
    }

}